var KOC = require("koc.common.utils");

var Config = require("../../config");
var ConfigAdmin = require("../../config.admin");

var StateData = require("../statedata");
var DALFactory = require("../../lib/dal/dal-factory");
var CommonUtils = require("../../lib/common-utils");

module.exports = {
    Ajax: require("./page.ajax"),
    Main: require("./page.main"),
    User_Manage: require('./page.user-manage'),
    Order_Manage: require('./page.order-manage'),
    Report_Manage: require('./page.report-manage'),
    Base_Manage: require('./page.base-manage'),
    ////////////////////////////////////////////
    //母版页
    ////////////////////////////////////////////
    Master: {
        Modal: function (page, code) {
            code = code === undefined ? "" : code;
            return function (req, res) {
                //输出常量
                res.locals.Virtual_Path = Config.Common.Virtual_Path;
                //输出状态
                CommonUtils.Common.PageScript_StateData(res, StateData.Value(req, res));
                //输出URL
                CommonUtils.Common.PageScript_VirtualPath(res, Config.Common.Virtual_Path);
                //判断权限
                if (StateData.RightCode.Admin(req, res, code) !== 0) {
                    return;
                }
                //读取通用参数
                var _ModalData = {
                    Tab: req.params.Tab,
                    EnterClose: req.params.EnterClose ? true : false
                };
                CommonUtils.Common.PageScript(res, "var _ModalData = " + JSON.stringify(_ModalData) + ";");
                //渲染页面
                res.locals.Title = "管理后台";
                page(req, res, function (err, html) {
                    if (err) {
                        //转到错误页面
                        return;
                    }
                    res.render("master/modal", {Content: html});
                });
            };
        },
        Main: function (page, code, menu) {
            code = code ? code : "";
            menu = menu ? menu : code;
            return function (req, res) {
                //输出常量
                res.locals.Virtual_Path = Config.Common.Virtual_Path;
                //输出状态
                CommonUtils.Common.PageScript_StateData(res, StateData.Value(req, res));
                //输出URL
                CommonUtils.Common.PageScript_VirtualPath(res, Config.Common.Virtual_Path);
                //判断权限
                switch (StateData.RightCode.Admin(req, res, code)) {
                    case 0:
                        break;
                    case -2:
                        res.redirect(Config.Common.Virtual_Path.Main);
                        return;
                    case -1:
                        res.redirect('/login');
                        return;
                    default:
                        res.redirect('/login');
                        return;
                }
                //读取菜单
                var _MenuLeft = '';
                var _MenuTop = '';
                KOC.Lodash.forEach(ConfigAdmin.MenuConfig, function (ThisValue) {
                    if (ThisValue.Hide) {
                        return true;
                    }
                    if (StateData.RightCode.Admin(req, res, ThisValue.RightCode) !== 0) {
                        return true;
                    }
                    ThisValue.MenuCode = ThisValue.MenuCode || ThisValue.RightCode;
                    var _MenuTop_Active = menu.indexOf(ThisValue.MenuCode) == 0;
                    _MenuTop += '<li class="' + (_MenuTop_Active ? 'active' : '') + ' hover">' +
                        '<a href="' + (ThisValue.Href ? ThisValue.Href : 'javascript:;') + '">' +
                        '<i class="menu-icon fa fa-' + ThisValue.Icon + '"></i>' +
                        '<span class="menu-text"> ' + ThisValue.Text + ' </span>' +
                        '</a>';
                    var _MenuTop_ChildShow = ThisValue.ChildShow;
                    if (ThisValue.Child && ThisValue.Child.length > 0) {
                        if (_MenuTop_ChildShow) {
                            _MenuTop += '<ul class="submenu">';
                        }
                        KOC.Lodash.forEach(ThisValue.Child, function (ThisValue) {
                            if (ThisValue.Hide) {
                                return true;
                            }
                            if (StateData.RightCode.Admin(req, res, ThisValue.RightCode) !== 0) {
                                return true;
                            }
                            ThisValue.MenuCode = ThisValue.MenuCode || ThisValue.RightCode;
                            var _Menu_Active = menu.indexOf(ThisValue.MenuCode) == 0;
                            if (_MenuTop_ChildShow) {
                                _MenuTop += '<li class="hover' + (_Menu_Active ? ' active' : '') + '">' +
                                    '<a href="' + (ThisValue.Href ? ThisValue.Href : 'javascript:;') + '">' +
                                    '<i class="menu-icon fa fa-caret-right"></i>' + ThisValue.Text +
                                    (ThisValue.Child && ThisValue.Child.length > 0 ? '<b class="arrow fa fa-angle-right"></b>' : '') +
                                    '</a>' +
                                    '<b class="arrow"></b>';
                            }
                            if (_MenuTop_Active) {
                                _MenuLeft += '<li class="highlight' + (_Menu_Active ? ' active' : '') + (ThisValue.Open !== false && ThisValue.Child && ThisValue.Child.length > 0 ? ' open' : '') + '">' +
                                    '<a href="' + (ThisValue.Href ? ThisValue.Href : 'javascript:;') + '"' + (ThisValue.Child && ThisValue.Child.length > 0 ? ' class="dropdown-toggle' : '') + '">' +
                                    '<i class="menu-icon fa fa-' + ThisValue.Icon + '"></i>' +
                                    '<span class="menu-text">' + ThisValue.Text + '</span>' +
                                    (ThisValue.Open !== false && ThisValue.Child && ThisValue.Child.length > 0 ? '<b class="arrow fa fa-angle-down"></b>' : '') +
                                    '</a>';
                            }
                            if (ThisValue.Child && ThisValue.Child.length > 0) {
                                if (_MenuTop_ChildShow) {
                                    _MenuTop += '<ul class="submenu">';
                                }
                                if (_MenuTop_Active) {
                                    _MenuLeft += '<ul class="submenu">';
                                }
                                KOC.Lodash.forEach(ThisValue.Child, function (ThisValue) {
                                    if (ThisValue.Hide) {
                                        return true;
                                    }
                                    if (StateData.RightCode.Admin(req, res, ThisValue.RightCode) !== 0) {
                                        return true;
                                    }
                                    ThisValue.MenuCode = ThisValue.MenuCode || ThisValue.RightCode;
                                    _Menu_Active = menu.indexOf(ThisValue.MenuCode) == 0;
                                    if (_MenuTop_ChildShow) {
                                        _MenuTop += '<li class="hover' + (_Menu_Active ? ' active' : '') + '">' +
                                            '<a href="' + (ThisValue.Href ? ThisValue.Href : 'javascript:;') + '">' +
                                            '<i class="menu-icon fa fa-caret-right"></i> ' + ThisValue.Text + '</a>' +
                                            '<b class="arrow"></b>' +
                                            '</li>';
                                    }
                                    if (_MenuTop_Active) {
                                        _MenuLeft += '<li' + (ThisValue.Open !== false && _Menu_Active ? ' class="active"' : "") + '>' +
                                            '<a href="' + ThisValue.Href + '"><i class="menu-icon fa fa-caret-right"></i>' + ThisValue.Text + '</a>' +
                                            '<b class="arrow"></b>' +
                                            '</li>';
                                    }
                                });
                                if (_MenuTop_ChildShow) {
                                    _MenuTop += '</ul>';
                                }
                                if (_MenuTop_Active) {
                                    _MenuLeft += '</ul>';
                                }
                            }
                            if (_MenuTop_ChildShow) {
                                _MenuTop += '</li>';
                            }
                            if (_MenuTop_Active) {
                                _MenuLeft += '</li>';
                            }
                        });
                        if (_MenuTop_ChildShow) {
                            _MenuTop += '</ul>';
                        }
                    }
                    _MenuTop += '</li>';
                });
                //渲染页面
                res.locals.Title = "管理后台";
                page(req, res, function (err, html) {
                    if (err) {
                        //转到错误页面
                        res.redirect("");
                        return;
                    }
                    res.render("master/main", {
                        SystemName: "后台管理系统",
                        MenuTop: _MenuTop,
                        MenuLeft: _MenuLeft,
                        Content: html
                    });
                });
            };
        }
    }
};